
module iTimer.ui.Event ;
import iTimer.Types ;




class iEventList {
    
	static Tree tree;
	static Menu menu;
    
    version(TEST111) : 
    static void load(Composite parent){
		Composite comp	= new Composite(parent, DWT.BORDER );
		comp.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WHITE));

		GridLayout layout = new GridLayout();
		layout.marginWidth = 2;
		layout.marginHeight = 2;
		layout.makeColumnsEqualWidth = true;
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 3;
		comp.setLayout(layout);
		comp.layout();
		
		CLabel cLabel5 = new CLabel(comp, DWT.NONE);
		GridData cLabel5LData	= new GridData();
		cLabel5LData.horizontalAlignment	= GridData.FILL;
		cLabel5LData.verticalAlignment	= GridData.FILL;
		cLabel5LData.grabExcessHorizontalSpace = true;
		cLabel5.setBackground(GShell.getDisplay().getSystemColor(DWT.COLOR_WIDGET_BACKGROUND));
		cLabel5.setText("定时器列表");
		//cLabel5.setImage(DWTResourceManager.getImage("SAMPLE", GIFFILE));
		cLabel5.setLayoutData(cLabel5LData);
		
		tree	=	new Tree(comp, DWT.NONE );
		
		GridData tree4LData = new GridData();
		tree4LData.grabExcessHorizontalSpace = true;
		tree4LData.grabExcessVerticalSpace = true;
		tree4LData.horizontalAlignment = GridData.FILL;
		tree4LData.verticalAlignment = GridData.FILL;
		tree.setLayoutData(tree4LData);
		
		CreateMenu;
		
		update(true);
		list;
	}
	
	static void CreateMenu(){
		menu		= new Menu (GShell , DWT.POP_UP); 
		tree.setMenu(menu);
		/*
			tree.addSelectionListener(new class SelectionListener{
				public void widgetSelected(SelectionEvent e){
					//version(DEV) log2(__FILE__, __LINE__, "已选择");
				}
				public void widgetDefaultSelected(SelectionEvent e){}
			});
		*/
		menu.addListener(DWT.Show, new class Listener{ 
			public void handleEvent (Event event) {
				int selected	= tree.getSelectionCount();
				//log2(__FILE__, __LINE__, "选择了: {}\n", selected);
				Thread.sleep(.1);
				if( selected is 0 ){
					menu.getItem(1).setEnabled(false);
					menu.getItem(2).setEnabled(false);
				}else{
					menu.getItem(1).setEnabled(true);
					menu.getItem(2).setEnabled(true);
				}
			} 
		}); 
	
		MenuItem ad	= new MenuItem(menu, DWT.PUSH);
		ad.setText("添加");
		ad.addListener(DWT.Selection, new class Listener{ 
			public void handleEvent (Event event) { 
				// SvrWin.Open;
			} 
		}); 
		
		MenuItem mo	= new MenuItem(menu, DWT.PUSH);
		mo.setText("修改");
		mo.addListener(DWT.Selection, new class Listener{ 
			public void handleEvent (Event event) {
                /*
				auto ns	= SvrList.tree.getSelection;
				if( ns.length ){
					auto	obj	= ns[0].getData("id");
					if( obj !is null ){
						auto id	= (cast(MyNode) obj) .id;
						SvrWin.Open( id );
					}
				}
                */
			}
		});
		MenuItem de	= new MenuItem(menu, DWT.PUSH);
		de.setText("删除");
		de.addListener(DWT.Selection, new class Listener{ 
			public void handleEvent (Event e) { 
                /++
				auto ns	= SvrList.tree.getSelection;
				if( ns.length ){
					auto	obj	= ns[0].getData("id");
					if( obj !is null ){
						auto id	= (cast(MyNode) obj) .id;
						scope stmt2	= Statement.Create(db_con);
						try{
							int ret	= MessageBox.showMessage("确定要删除私服 `" ~  ns[0].getText ~ "` 吗？\n\n 删除后会把账号一起删除！", "提示", GShell, DWT.YES | DWT.NO  );
							if( ret == DWT.YES ){
								/*
								SSvr0.Delete(stmt2, id);
								scope char[] del_sql2	= "DELETE FROM role0 WHERE iName = " ~ Int.toString(id) ;
								db_con.exec(del_sql2);
								delete	stmt2;
								list;
								update;
								*/
								MessageBox.showMessage("删除暂时禁止使用！", "提示", GShell);
							}
						}catch(Exception e){
							log2(__FILE__, __LINE__, "throw: {}\n",  e.msg);
						}
					}
				}
            ++/
			}
		});
	}
	static public final class MyNode{
		int	__id;
		this(int _id){
			__id	= _id;
		}
		int id(){
			return __id;
		}
		void id(int _id){
			__id	= _id;
		}
	}
	static void list(int id = -1 ){
		tree.removeAll();
        /++
		SUM.sLoad;
		auto sl_ptr	= SUM.s.load();
		uint	len	= sl_ptr.list.length ;
		
		TreeItem[] CurItems;
		foreach(int _ii, p; sl_ptr.list ){
			auto item	= new TreeItem(tree, DWT.NONE);
			item.setText(  p.sName );
			item.setData("id", new MyNode(p.id) );
			if( id == p.id ){
				CurItems	~= item;
			}
		}
		if( CurItems.length ){
			tree.deselectAll;
			tree.setSelection( CurItems );
		}
        ++/
	}
	
	static void update( bool isSkipWrite = false ){
        /++
		SUM.sLoad;
		if( isSkipWrite ){
			return ;
		}
		auto sl_ptr	= SUM.s.load();
		uint	len	= sl_ptr.list.length ;
		
		scope buf 		= new Array(1024, 1024);
		buf("[main]\nname=本地私服\n\n[Group1]\n");
		buf("GroupNum=")( Int.toString(len) )("\n");
		foreach(int _ii, p; sl_ptr.list ){
			char[] ii	= Int.toString(_ii+1);
			buf("Group")(ii)("=")(p.sName)("\n");
			buf("GroupNick")(ii)("=")(p.sName)("\n");
		}
		RoleList.list;
		buf("ServerAddr=")(ProxyIp)("\n");
		
		try{
			char[] dat	= cast(char[]) buf.slice;
			auto dat2	= u82gb(dat);
			char[] _file	= SelfDir[0..$-1] ~ r"addons\setting\Default\group.ini";
			File.set(_file, dat2);
		}catch(Exception  e){
			MessageBox.showMessage("目录错误！", "提示", GShell);
			log2(__FILE__, __LINE__, "throw : `{}`", e.msg);
		}
            ++/
	}
    
}